﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using Newtonsoft.Json;
using WebSocket4Net;
using WinCustomsService.Model;


namespace WinCustomsService
{
    /// <summary>
    /// 
    /// </summary>
    public class Customs311Job : ISyncData
    {
        string xmlStr = string.Empty;
       
        CustomsPustSetting setting = new CustomsPustSetting();
        Push311Info info = new Push311Info();
        public void SyncData()
        {
            setting = HttpClientHelper.GetSetting();

            var list = HttpClientHelper.GetPush311Xml();

            list.ForEach(item =>
            {
                info = item;
                Push311(item);
            });

        }
       
        /// <summary>
        /// 推送订单
        /// </summary>
        private void Push311(Push311Info model)
        {

            try
            {
                CustomsClearanceEditor customs = new CustomsClearanceEditor();

                customs.OrderId = model.OrderId;
                customs.Id = model.Id;
                customs.Passwd = setting.Passwd;
                customs.XmlStr = model.XmlStr;
                customs.TypeId = CustomsPushPlatform.Customs;
                customs.Message = string.Empty;
                customs.Data = string.Empty;
                customs.OrderStatus = CustomsPushType.fail;
                customs.PushType = CustomsPushType.fail;
                customs.VerifyDepartment = string.Empty;
                customs.VerifyDepartmentTradeId = string.Empty;
                customs.Cert_check_result = string.Empty;
                LogHelper.WriteLog($"customs:" + JsonConvert.SerializeObject(customs));
                string xmlstr = Sign311Helper.GetSignature(customs);
                LogHelper.WriteLog($"xmlstr:" + xmlstr);
                if(string.IsNullOrEmpty(xmlstr))
                    return;
                Byte[] bytes = Encoding.UTF8.GetBytes(xmlstr);
                string xmlstr64 = Convert.ToBase64String(bytes);

                if(!string.IsNullOrEmpty(customs.Message))
                {
                    customs.OrderStatus = CustomsPushType.fail;
                    customs.PushType = CustomsPushType.fail;

                }
               

                string SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //发起请求
                Push311Submit submit = new Push311Submit()
                {
                    Message = new Push311Submit.MessageInfo()
                    {
                        MessageHead = new Push311Submit.MessageHeadInfo()
                        {
                            SendTime = SendTime
                        },
                        MessageBody = new Push311Submit.MessageBodyInfo()
                        {
                            data = xmlstr64
                        }
                    }
                };
                submit.Message.MessageHead.MessageType = model.MessageType;
                customs.Data = JsonConvert.SerializeObject(submit)+ "xmlstr:" + xmlstr;

                string content = HttpClientHelper.UploadCustoms311(submit);
                customs.Message = SendTime;
                if(content== "OK")
                {
                    customs.OrderStatus = CustomsPushType.Pushed;
                    customs.PushType = CustomsPushType.Pushed;
                }
                else
                {
                    customs.Error = content;
                 
                }

               // LogHelper.WriteLog("customs:" + JsonConvert.SerializeObject(customs));
                HttpClientHelper.CustomsCreate(customs);



            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("需求请求数据", ex);
            }
        }





    }
}
